package com.xz.questions.model.qianwen;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
public class QianWenChat {
    public static GenerationResult callWithMessage(String question) throws NoApiKeyException, ApiException, InputRequiredException
    {
        String url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
        // String apiKey = "";//这里换成你自己的ApiKey

        Constants.apiKey="sk-24ed8c7498404737875faaeeb13e040b";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("请问杭州阿里巴巴工作环境咋样？").build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_V1).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = gen.call(param);
        log.info("千问api回答的话语为：{}",result);
        System.out.println(result);
        return result;
    }
}
